﻿using FictionsDownload.DAL;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FictionsDownload.BLL
{
    public class Directory : BaseBLL
    {
        public void Delete(DirectoryModel directory)
        {
            directory.Delete(dal);
        }
        public bool Update(DirectoryModel directoryModel)
        {
            //Book数据校验
            if (directoryModel.Name == "" || directoryModel.Name == null)
            {
                Message = "名称为空";
                return false;
            }
            if (directoryModel.Url == "" || directoryModel.Url == null)
            {
                Message = "Url为空";
                return false;
            }
            directoryModel.ActiveTime = DateTime.Now;
            dal.Update(directoryModel);
            return true;
        }
        public bool Add(DirectoryModel directoryModel)
        {
            //同一本书中没有相同的Url
            var dirs = All<DirectoryModel>().Where(d => d.Url == directoryModel.Url && d.BookId == directoryModel.BookId);
            if (dirs.Count() == 0)
            {
                dal.Insert(directoryModel);
                return true;
            }
            return false;
        }
        public void Add(List<DirectoryModel> directories)
        {
            List<DirectoryModel> tempDirectory = GetList<DirectoryModel>();
            foreach (var dir in directories)
            {
                if (HttpHelper.UrlParse(dir.Url) == null)
                {
                    Message = dir.Url + "不是正确的网址";
                }
                Add(dir);
            }
        }
        /// <summary>
        /// 添加这本书的目录
        /// </summary>
        /// <param name="keyValuePairs"></param>
        public void Add(BookModel book,Dictionary<string, string> keyValuePairs)
        {
            Uri uri = new Uri(string.IsNullOrEmpty(book.DirectoryUrl) ? book.Url : book.DirectoryUrl);
            var dirs = this.GetList<DirectoryModel>(m => m.BookId == book.Id);
            int sortIndex = dirs.Any() ? dirs.Max(m => m.SortIndex) : 0;
            foreach (var keyValue in keyValuePairs)
            {
                string tempUrl = "";
                if (keyValue.Key.Count() >= 2 && keyValue.Key.Substring(0, 2) == "//")
                {
                    tempUrl = "https:" + keyValue.Key;
                }
                else if (keyValue.Key.Count() >= 1 && keyValue.Key.Substring(0, 1) == "/")
                {
                    tempUrl = uri.Scheme + "://" + uri.DnsSafeHost + keyValue.Key;
                }
                else if (keyValue.Key.Contains("http"))
                {
                    tempUrl = keyValue.Key;
                }
                else
                {
                    tempUrl = uri + keyValue.Key;
                }
                Add(new DirectoryModel
                {
                    Name = keyValue.Value,
                    Url = tempUrl,
                    BookId = book.Id,
                    SortIndex = ++sortIndex,
                });
            }
        }
    }
}
